﻿using Nectar.DAL.Common;
using P2PWeb.Common;
using P2PWeb.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace P2PWeb.Controllers
{
    [AccountAuthorize(AccountType.Admin,AccountType.LookThrough)]
    public class AccountManagerController : BaseController
    {
        static Repository<P2PModelContainer> _context = new Repository<P2PModelContainer>();
        //
        // GET: /AccountManager/
        public ActionResult Index(int pageIndex=1)
        {
            Pager<Account> pager = new Pager<Account>();
            pager.CurrentPageIndex = pageIndex;


            QueryParameter isDelParameter = new QueryParameter("isDel", false);
            QueryParameter[] pageParameters = pager.GetQueryParameters();
            pager.Items = _context.GetListOf<Account>("o.IsDel=@isDel and o.Status=1 order by o.CreationTime desc  Skip @Skip Limit @Limit", pageParameters.Union(new[] { isDelParameter }).ToArray()
                , new List<string>(new[] { "AccountAuthentication", "AccountInfo" })).ToList();
            pager.TotialItemsCount = _context.GetListOf<Account>("o.IsDel=@isDel and o.Status=1 order by o.CreationTime desc", new[] { isDelParameter }).Count;
            if (Request.IsAjaxRequest())
            {
                return PartialView("AccountManagerAjax", pager);
            }
            return View(pager);
        }

        public ActionResult AccountDetail(int accountId)
        {
            QueryParameter isDelParameter = new QueryParameter("isDel", false);
            QueryParameter iDParameter = new QueryParameter("ID", accountId);
            return View(_context.GetListOf<Account>("o.ID=@ID and o.IsDel=@isDel", new[] { isDelParameter, iDParameter }, new List<string>() { "AccountAuthentication", 
                "AccountInfo", "WorkInfo", "ContactInfo" })
                .First());
        }

        private void ChangingAccountStateToPass(Account account)
        {
            if (account.Status != (int)AccountState.LookThrough)
                throw new Exception("非法操作");
            account.Status = (int)AccountState.Pass;
        }

        private void ChangingAccountStateToUnfit(Account account)
        {
            if (account.Status != (int)AccountState.LookThrough)
                throw new Exception("非法操作");
            account.Status = (int)AccountState.Unfit;
        }

        [ValidateAntiForgeryToken]
        public ActionResult ChangeAccountAuthenticationState(int ID, int state)
        {
            Account account = _context.GetEntityByID<Account>(ID);
            switch ((AccountState)state)
            {
                case AccountState.Pass:
                    ChangingAccountStateToPass(account);
                    break;
                case AccountState.Unfit:
                    ChangingAccountStateToUnfit(account);
                    break;
                default:
                    throw new Exception("操作非法");
            }
            _context.Update(account);
            return RedirectToAction("index");
        }
    }
}
